﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM.DataObjects;
using System.Data;

namespace SRM
{
    public partial class SendNewPasswordForm : System.Web.UI.Page
    {
        public DataTable Applicant { get; set; }
        public string ApplicantKey { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Send_New_Password_Button_Click(object sender, EventArgs e)
        {
            ErrorMessage_Literal.Text = string.Empty;
            // Compute MD5 hash for input password
            
            // Fetch record in database
            string emailAddress = Email_TextBox.Text.Trim();

            DataTable dtApplicantAccount = SRM_Applicant.GetApplicantAccount(emailAddress);
            if ((dtApplicantAccount != null) && (dtApplicantAccount.Rows.Count > 0))
            {
                // Compare MD5
                // Generate new password 
                string newPassword = Help.GenerateNewPassword();
                string newPasswordMD5 = Help.ComputeMD5(newPassword);

                // Set new password in account
                SRM_Applicant.SetApplicantAccountPassword(emailAddress, newPasswordMD5);

                // E-mail new password
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendLine("<p>Dear Sir/Madam,</p>");
                sb.AppendLine("<p>This is an automated e-mail sent out in response to your request for a new password to your SRM account.</p>");
                sb.AppendLine("<p>Your new password to your SRM account have been set to:</p>");
                sb.AppendFormat("<p style='font-weight:bold'>{0}</p>\r\n", newPassword);
                sb.AppendLine("<p>Please use this password to log in to your SRM account in the future</p>");
                sb.AppendLine("<p>From<br/>SRM System</p>");

                Help.SendEmail(emailAddress, "SRM Account New Password", sb.ToString(), true);

                ErrorMessage_Literal.Text = "<span>New password sent.</span>";
            }
            else
            {
                // Display error message
                ErrorMessage_Literal.Text = "<span class=\"warning\">Invalid email address.</span>";
            }

            


        }
} // public partial class ApplicantForm : System.Web.UI.Page

} // namespace SRM